package com.hs.egova.base;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author husheng
 * @version v1.0
 * @date 2021/7/26 11:01
 * @email husheng@egova.com.cn
 */
@ApiModel("返回数据")
public class ResultInfo {

    @ApiModelProperty("操作是否成功")
    protected boolean success;
    @ApiModelProperty("结果代码")
    protected Integer code;
    @ApiModelProperty("错误信息")
    protected String message;
    @ApiModelProperty("结果数据")
    protected Map<String, Object> data;
    protected Integer tipType;

    public ResultInfo() {
        this.success = false;
        this.code = 0;
        this.message = null;
        this.data = new HashMap<>();
        this.tipType = 1;
    }

    public ResultInfo(boolean success) {
        this();
        this.success = success;
    }
    public ResultInfo(String key, Object object){
        this();
        this.success = true;
        this.data.put(key, object);
    }
    public ResultInfo(int code) {
        this();
        this.code = code;
    }

    public ResultInfo(boolean success, int code) {
        this();
        this.success = success;
        this.code = code;
    }
    public ResultInfo(boolean success, String message) {
        this();
        this.success = success;
        this.message = message;
    }
    public ResultInfo(boolean success, String message, Map<String, Object > data) {
        this();
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
    public void setData(String key, Object object) {
        this.data.put(key, object);
    }
    public void addData(String key, Object object) {
        this.data.put(key, object);
    }
    public Object getData(String key) {
        return this.data.get(key);
    }

    public Integer getTipType() {
        return tipType;
    }

    public void setTipType(Integer tipType) {
        this.tipType = tipType;
    }
    public <T> T getDataValue(String key, Class<T> clazz) {
        Object dataValue = this.data.get(key);
        return dataValue == null ? null : JSON.parseObject(JSON.toJSONString(dataValue), clazz);
    }

    public <T> List<T> getDataListValue(String key, Class<T> clazz) {
        Object dataValue = this.data.get(key);
        return (List)dataValue == null ?new ArrayList<>() : JSON.parseArray(JSON.toJSONString(dataValue), clazz);
    }
}
